package fucai.com.fucai.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import fucai.com.fucai.R;
import fucai.com.fucai.utils.MainUsedInterface;

/**
 * @说 明： 仿照写的
 * @项目名称：fucai
 * @包 名： fucai.com.fucai.db
 * @类 名： Fucai
 * @创 建人： chenshuangniu
 * @创建时间：2015/12/17 19:45
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class TitleBar extends LinearLayout implements View.OnClickListener {
    @ViewInject(R.id.title)
    private TextView mTitle = null;
    @ViewInject(R.id.left_text_btn)
    private Button mLeftTextBtn = null;
    @ViewInject(R.id.right_text_btn)
    private Button mRightTextBtn = null;
    @ViewInject(R.id.left_image_btn)
    private ImageButton mLeftImageBtn = null;
    @ViewInject(R.id.right_image_btn)
    private ImageButton mRightImageBtn = null;

    private MainUsedInterface.MainButtonClickListener mMainClickListener = null;
    private String mLeftButtonName = "";
    private String mRightButtonName = "";

    public TitleBar(Context context) {
        super(context);
        initUi(context);
    }


    public void setmMainClickListener(MainUsedInterface.MainButtonClickListener listener, String leftButtonName, String rightButtonName) {
        this.mMainClickListener = listener;
        this.mLeftButtonName = leftButtonName;
        this.mRightButtonName = rightButtonName;
    }

    public void initUi(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.title_view, null);
        this.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ViewUtils.inject(this, view);
    }

    //设置信息
    public void initTitleBarInfo(String title, String leftBtnText, String rightBtnText) {
        mTitle.setText(title);
        if (!leftBtnText.equals("")) {
            mLeftTextBtn.setText(leftBtnText);
            mLeftTextBtn.setVisibility(View.VISIBLE);
            if (mLeftTextBtn.getText().toString().length() > 2) {
                mLeftTextBtn.setPadding(0, 0, 16, 0);
            }
            mLeftTextBtn.setOnClickListener(this);
        } else {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
        }

        if (!rightBtnText.equals("")) {
            mRightTextBtn.setText(rightBtnText);
            mRightTextBtn.setVisibility(View.VISIBLE);
            mRightTextBtn.setOnClickListener(this);
        } else {
            mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);
        }
    }

    public String getTitle() {
        return mTitle.getText().toString();
    }

    /**
     * 回调点击注册时传进来的值
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (null == mMainClickListener) {
            return;
        }
        if (v.getId() == R.id.left_image_btn || v.getId() == R.id.left_text_btn) {
            mMainClickListener.clickButtonListener(mLeftButtonName);
        } else {
            mMainClickListener.clickButtonListener(mRightButtonName);
        }
    }
}
